package ua.kpi.fpm.km61.strat.sp.rules;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Chain {

    private List<GramSymbol> out = new ArrayList<GramSymbol>();
    private Set<Terminal> first  = new HashSet<Terminal>();
    private boolean flag;
    
    public Chain() {
	super();
	first.add(new Terminal("Esc"));
    }

    public void calculate () {
        if ( flag ){
            return ;
        }
        flag = true ;
        for ( GramSymbol symbol : this.getOut() ) {
            if ( symbol.getClass().equals(Terminal.class)) {
                first.add((Terminal) symbol ) ;
                return ;
            }
            NonTerminal nonTerm = (NonTerminal) symbol ;
            first.addAll(nonTerm.getFirst()) ;
            if ( !(nonTerm.isEps())){
        	return ; 
            } 
        }
    }

    public List<GramSymbol> getOut() {
        return out;
    }

    public void setOut(List<GramSymbol> out) {
        this.out = out;
    }
    
    
}
